﻿namespace TfsExtensions.Build.ActivityLibrary.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using TfsExtensions.Build.Activities;
    using System.Activities;
    using System.Collections.ObjectModel;
    using System.Reflection;

    [TestClass]
    public class SandcastleDocumentationTest
    {
        public string HtmlWorkshopInstallationFolder { get; set; }
        public string SandCastleInstallationDirectory { get; set; }


        [TestMethod]
        public void InvokeDocumentationActivityWithNoSelectedProjects()
        {
            this.CreateSandCastleInstallationDirectoryStructure();
            this.CreateHtmlWorkshopDirectoryStructure();

            var workflow = new InvokeSandCastleDocumentation();

            Dictionary<string, object> parameters = new Dictionary<string, object>();
            
            parameters.Add("ChmFileName", "Test");
            parameters.Add("HtmlHelpWorkshopInstallationFolder", this.HtmlWorkshopInstallationFolder);
            parameters.Add("InputDirectoryWithBinariesToProcess", Environment.CurrentDirectory);
            parameters.Add("SandCastleInstallationDirectory", this.SandCastleInstallationDirectory);
            parameters.Add("OutputDirectory", Environment.CurrentDirectory);
            
            var outParams = WorkflowInvoker.Invoke(workflow, parameters);
            if(outParams != null && outParams.ContainsKey("CommentAssemblyFileNames"))
            {
                var listAssemblies = (Collection<string>)outParams["CommentAssemblyFileNames"];
            }
            
        }

        [TestMethod]
        public void InvokeDocumentationActivityWithSelectedProjects()
        {
            this.CreateSandCastleInstallationDirectoryStructure();
            this.CreateHtmlWorkshopDirectoryStructure();

            var workflow = new InvokeSandCastleDocumentation();

            //Ensure Assemblies and Xml Comments for the same
            File.AppendAllText(Path.Combine(Environment.CurrentDirectory, "Sample.dll"),"SampleContent");
            File.AppendAllText(Path.Combine(Environment.CurrentDirectory, "Sample.xml"), "SampleContent");

            File.AppendAllText(Path.Combine(Environment.CurrentDirectory, "SampleExe.exe"), "SampleContent");
            File.AppendAllText(Path.Combine(Environment.CurrentDirectory, "SampleExe.xml"), "SampleContent");

            Dictionary<string, object> parameters = new Dictionary<string, object>();

            parameters.Add("ChmFileName", "Test");
            parameters.Add("HtmlHelpWorkshopInstallationFolder", this.HtmlWorkshopInstallationFolder);
            parameters.Add("InputDirectoryWithBinariesToProcess", Environment.CurrentDirectory);
            parameters.Add("SandCastleInstallationDirectory", this.SandCastleInstallationDirectory);
            parameters.Add("OutputDirectory", Environment.CurrentDirectory);
            
            var outParams = WorkflowInvoker.Invoke(workflow, parameters);
            if (outParams != null && outParams.ContainsKey("CommentAssemblyFileNames"))
            {
                var listAssemblies = (Collection<string>)outParams["CommentAssemblyFileNames"];
            }

        }

        private void CreateSandCastleInstallationDirectoryStructure()
        {
            var programFileDirectory = Environment.CurrentDirectory;
           
            if (!string.IsNullOrEmpty(programFileDirectory))
            {
                var sandCastleDirectoryInfo = Directory.CreateDirectory(Path.Combine(programFileDirectory,"Sandcastle1"));

                this.SandCastleInstallationDirectory = sandCastleDirectoryInfo.FullName;

                // Data folder and its subfolder
                var sandCastleDataDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastleDirectoryInfo.FullName, "Data"));
                var sandCastleDataReflectionDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastleDataDirectoryInfo.FullName, "Reflection"));


                // Examples folder and its subfolder
                var sandCastleExamplesDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastleDirectoryInfo.FullName, "Examples"));
                
                var sandCastleExamplesConceptualExampleDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastleExamplesDirectoryInfo.FullName, "conceptualExample"));
                var sandCastleExamplesConceptualExampleDduexmlDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastleExamplesConceptualExampleDirectoryInfo.FullName, "dduexml"));
                var sandCastleExamplesConceptualExampleExtractedfilesDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastleExamplesConceptualExampleDirectoryInfo.FullName, "extractedfiles"));
                var sandCastleExamplesConceptualExampleXMLCompDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastleExamplesConceptualExampleDirectoryInfo.FullName, "xmlcomp"));

                var sandCastleExamplesGenericDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastleExamplesDirectoryInfo.FullName, "generic"));
                var sandCastleExamplesSandcastleDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastleExamplesDirectoryInfo.FullName, "sandcastle"));

                // Presentation folder and its subfolder
                var sandCastlePresentationDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastleDirectoryInfo.FullName, "Presentation"));
                var sandCastlePresentationHanaDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationDirectoryInfo.FullName, "hana"));
                var sandCastlePresentationHanaconfigurationDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationHanaDirectoryInfo.FullName, "configuration"));
                var sandCastlePresentationHanaContentDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationHanaDirectoryInfo.FullName, "Content"));
                var sandCastlePresentationHanaiconsDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationHanaDirectoryInfo.FullName, "icons"));
                var sandCastlePresentationHanaScriptsDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationHanaDirectoryInfo.FullName, "Scripts"));
                var sandCastlePresentationHanaStylesDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationHanaDirectoryInfo.FullName, "Styles"));
                var sandCastlePresentationHanaTransformsDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationHanaDirectoryInfo.FullName, "transforms"));

                var sandCastlePresentationPrototypeDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationDirectoryInfo.FullName, "Prototype"));
                var sandCastlePresentationPrototypeConfigurationDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationPrototypeDirectoryInfo.FullName, "configuration"));
                var sandCastlePresentationPrototypeContentDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationPrototypeDirectoryInfo.FullName, "content"));
                var sandCastlePresentationPrototypeIconsDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationPrototypeDirectoryInfo.FullName, "icons"));
                var sandCastlePresentationPrototypeScriptsDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationPrototypeDirectoryInfo.FullName, "scripts"));
                var sandCastlePresentationPrototypeStylesDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationPrototypeDirectoryInfo.FullName, "styles"));
                var sandCastlePresentationPrototypeTransformsDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationPrototypeDirectoryInfo.FullName, "transforms"));


                var sandCastlePresentationSharedDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationDirectoryInfo.FullName, "Shared"));
                var sandCastlePresentationSharedConfigurationDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationSharedDirectoryInfo.FullName, "configuration"));
                var sandCastlePresentationSharedContentDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationSharedDirectoryInfo.FullName, "content"));
                var sandCastlePresentationSharedHxsTemplateDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationSharedDirectoryInfo.FullName, "HxsTemplate"));
                var sandCastlePresentationSharedScriptsDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationSharedDirectoryInfo.FullName, "scripts"));
                var sandCastlePresentationSharedTransformsDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationSharedDirectoryInfo.FullName, "transforms"));


                var sandCastlePresentationvs2005DirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationDirectoryInfo.FullName, "vs2005"));
                var sandCastlePresentationvs2005ConfigurationDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationvs2005DirectoryInfo.FullName, "configuration"));
                var sandCastlePresentationvs2005ContentDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationvs2005DirectoryInfo.FullName, "content"));
                var sandCastlePresentationvs2005IconsDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationvs2005DirectoryInfo.FullName, "icons"));
                var sandCastlePresentationvs2005ScriptsDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationvs2005DirectoryInfo.FullName, "scripts"));
                var sandCastlePresentationvs2005StylesDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationvs2005DirectoryInfo.FullName, "styles"));
                var sandCastlePresentationvs2005TransformsDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationvs2005DirectoryInfo.FullName, "transforms"));
                var sandCastlePresentationvs2005WebsiteDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastlePresentationvs2005DirectoryInfo.FullName, "website"));

                // ProductionTools folder
                var sandCastleProductionToolsDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastleDirectoryInfo.FullName, "ProductionTools"));

                this.CreateFile(sandCastleProductionToolsDirectoryInfo.FullName, "Mrefbuilder.exe", DocumentationResource.Mrefbuilder);
                this.CreateFile(sandCastleProductionToolsDirectoryInfo.FullName, "ChmBuilder.exe", DocumentationResource.ChmBuilder);
                this.CreateFile(sandCastleProductionToolsDirectoryInfo.FullName, "DBCSFix.exe", DocumentationResource.DBCSFix);
                this.CreateFile(sandCastleProductionToolsDirectoryInfo.FullName, "XslTransform.exe", DocumentationResource.XslTransform);

                
                // ProductionTransforms folder
                var sandCastleProductionTransformsDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastleDirectoryInfo.FullName, "ProductionTransforms"));
                
                // Schemas folder and its subfolder
                var sandCastleSchemasDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastleDirectoryInfo.FullName, "Schemas"));
                var sandCastleSchemasAuthoringDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastleSchemasDirectoryInfo.FullName, "Authoring"));
                var sandCastleSchemasReflectionDirectoryInfo = Directory.CreateDirectory(Path.Combine(sandCastleSchemasDirectoryInfo.FullName, "Reflection"));
            }
        }

        private void CreateFile(string path, string fileName,byte[] fileContent)
        {
            var stream = File.Create(Path.Combine(path, fileName), fileContent.Length);
            stream.Write(fileContent, 0, fileContent.Length);
            stream.Close();
        }
        private void CreateHtmlWorkshopDirectoryStructure()
        {
            var programFileDirectory = Environment.CurrentDirectory;

            if (!string.IsNullOrEmpty(programFileDirectory))
            {
                var htmlWorkshopDirectoryInfo = Directory.CreateDirectory(Path.Combine(programFileDirectory, "HTML Help Workshop1"));
                this.HtmlWorkshopInstallationFolder = htmlWorkshopDirectoryInfo.FullName;
                this.CreateFile(this.HtmlWorkshopInstallationFolder, "hhc.exe", DocumentationResource.hhc);
                var htmlWorkshopIncludeDirectoryInfo = Directory.CreateDirectory(Path.Combine(htmlWorkshopDirectoryInfo.FullName, "include"));
                var htmlWorkshopJavaDirectoryInfo = Directory.CreateDirectory(Path.Combine(htmlWorkshopDirectoryInfo.FullName, "java"));
                var htmlWorkshopLibDirectoryInfo = Directory.CreateDirectory(Path.Combine(htmlWorkshopDirectoryInfo.FullName, "lib"));
                var htmlWorkshopRedistDirectoryInfo = Directory.CreateDirectory(Path.Combine(htmlWorkshopDirectoryInfo.FullName, "redist"));
            }
        }
    }
}
